package com.deyuanyun.pic.ae.service.impl;

import com.deyuanyun.pic.ae.controller.dto.AEBaseQuery;
import com.deyuanyun.pic.ae.domain.SpecificPlace;
import com.deyuanyun.pic.ae.domain.SpecificPlaceExample;
import com.deyuanyun.pic.ae.enumeration.DataStatedEnum;
import com.deyuanyun.pic.ae.enumeration.IsNewDateEnum;
import com.deyuanyun.pic.ae.mapper.SpecificPlaceMapper;
import com.deyuanyun.pic.ae.mapper.dao.SpecificPlaceDao;
import com.deyuanyun.pic.ae.service.AEBaseService;
import com.deyuanyun.pic.ae.service.SpecificPlaceService;
import com.deyuanyun.pic.common.util.CommonUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.settings.support.biz.AttachmentUtil;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * “沿线特定场所” 服务接口 实现类
 *
 * @author huanglw
 * @date  2016-11-11
 *
 * History: [修改人][时间][修改描述]
 */
@Service
public class SpecificPlaceServiceImpl extends AEBaseService implements SpecificPlaceService {

    @Resource
    private SpecificPlaceMapper specificPlaceMapper;
    @Resource
    private SpecificPlaceDao specificPlaceDao;

    @Override
    public Page<SpecificPlace> query(AEBaseQuery aeBaseQuery) {
        return specificPlaceDao.query(aeBaseQuery);
    }

    @Override
    public SpecificPlace find(String id) {
        return specificPlaceMapper.selectByPrimaryKey(id);
    }

    @Override
    public int edit(SpecificPlace specificPlace, List<String> imgs) {

        // 计算交叉绝对里程
        PositionObj positionObj = getPositionInfo(specificPlace.getPipeLineId(), specificPlace.getPositionX(), specificPlace.getPositionY());
        if (positionObj != null) {
            specificPlace.setMileage(positionObj.getMileage());
            specificPlace.setNagm(positionObj.getNagm());
            specificPlace.setNagmNumber(positionObj.getNagmNumber());
            specificPlace.setNagmMileage(positionObj.getNagmMileage());
        }

        // 添加 图片信息
        String bizzId = StringUtils.isNotNullEmpty(specificPlace.getId()) ? specificPlace.getId() : CommonUtil.getUUID();
        for (String id : imgs) {
            AttachmentUtil.updateBusinessId(bizzId, id);
        }

        if (StringUtils.isNullEmpty(specificPlace.getId())){
            SpecificPlaceExample specificPlaceExample = new SpecificPlaceExample();
            specificPlaceExample.createCriteria().andDeletedEqualTo(Short.valueOf("0")).andNameEqualTo(specificPlace.getName());
            int countNum = specificPlaceMapper.countByExample(specificPlaceExample);
            Asserts.assertLogic(countNum > 0, "名称为:“"+specificPlace.getName()+"”沿线特定场所 已经存在，不能重复插入");

            specificPlace.setId(bizzId);
            specificPlace.setCreateDate(new Date());
            specificPlace.setCreateUser(realName);
            specificPlace.setDataStated(DataStatedEnum.ALREADY_RELEASED.getCode());
            specificPlace.setIsNewest(Short.valueOf(IsNewDateEnum.YES.getCode()));
            return specificPlaceMapper.insertSelective(specificPlace);
        }

        SpecificPlace specificPlaceObj = this.find(specificPlace.getId());
        Asserts.assertLogic(specificPlaceObj == null, "沿线特定场所 不存在");
        Asserts.assertLogic(isAuditReleased(specificPlaceObj.getDataStated()), "该 沿线特定场所 已审核通过,不允许编辑");
        if (isAuditDismissed(specificPlaceObj.getDataStated())){
            specificPlace.setDataStated(DataStatedEnum.ALREADY_RELEASED.getCode());
        }
        specificPlace.setUpdateDate(new Date());
        specificPlace.setUpdateUser(realName);
        specificPlace.setUpdateRemark("修改");
        return specificPlaceMapper.updateByPrimaryKeySelective(specificPlace);
    }

    @Override
    public int audit(SpecificPlace specificPlace) {

        SpecificPlace specificPlaceObj = this.find(specificPlace.getId());
        Asserts.assertLogic(specificPlaceObj == null, "沿线特定场所 不存在");
        Asserts.assertLogic(isAudit(specificPlaceObj.getDataStated()), "该 沿线特定场所 已审核,不能重复审核");
        return specificPlaceMapper.updateByPrimaryKeySelective(specificPlace);
    }

    @Override
    public int delete(String id) {
        SpecificPlace specificPlace = this.find(id);
        Asserts.assertLogic(specificPlace == null, "沿线特定场所 为空");
        Asserts.assertLogic(isAudit(specificPlace.getDataStated()), "该 沿线特定场所 已审核通过,不能“删除”");
        specificPlace = new SpecificPlace();
        specificPlace.setId(id);
        specificPlace.setDeleted(Short.valueOf("1"));
        specificPlace.setUpdateDate(new Date());
        specificPlace.setUpdateUser(realName);
        specificPlace.setUpdateRemark("删除");
        return specificPlaceMapper.updateByPrimaryKeySelective(specificPlace);
    }
}
